package com.awcall.iot.controller;


import com.awcall.iot.common.ResponseResult;
import com.awcall.iot.service.IFianceTotalService;
import com.awcall.iot.service.IStatementsService;
import com.awcall.iot.model.FianceTotal;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import net.sf.jsqlparser.schema.Column;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.awcall.iot.common.BaseController;

import java.util.List;

/**
 * <p>
 * 财务系统管理表 前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2018-03-29
 */
@RestController
@RequestMapping("/fianceTotal")
public class FianceTotalController extends BaseController {

    @Autowired
    private IFianceTotalService iFianceTotalService;


    @GetMapping("/index")
    public ResponseResult index(FianceTotal fianceTotal, Page<FianceTotal> page){

        EntityWrapper<FianceTotal> entityWrapper = new EntityWrapper<>();


        if(!StringUtils.isEmpty(fianceTotal.getPaytime())){
            entityWrapper.gt(FianceTotal.PAYTIME,fianceTotal.getPaytime());
        }

        if(!StringUtils.isEmpty(fianceTotal.getEndtime())){
            entityWrapper.lt(FianceTotal.ENDTIME,fianceTotal.getEndtime());
        }

        if(!StringUtils.isEmpty(fianceTotal.getName())){
            entityWrapper.like(FianceTotal.NAME,fianceTotal.getName());
        }

        if(!StringUtils.isEmpty(fianceTotal.getUserName())){
            entityWrapper.like(FianceTotal.USER_NAME,fianceTotal.getUserName());
        }

        if(!StringUtils.isEmpty(fianceTotal.getCompany())){
            entityWrapper.like(FianceTotal.COMPANY,fianceTotal.getCompany());
        }

        if(!StringUtils.isEmpty(fianceTotal.getType())){
            entityWrapper.eq("type", fianceTotal.getType());
        }

        if(!StringUtils.isEmpty(fianceTotal.getTelephone())){
            entityWrapper.like("telephone", fianceTotal.getTelephone());
        }


        Page<FianceTotal> userPage = iFianceTotalService.selectPage(page,entityWrapper);
        return new ResponseResult<>(userPage);
    }

    @GetMapping("/list")
    public ResponseResult list(FianceTotal fianceTotal){

        EntityWrapper<FianceTotal> entityWrapper = new EntityWrapper<>();

        if(!StringUtils.isEmpty(fianceTotal.getName())){
            entityWrapper.like(FianceTotal.NAME,fianceTotal.getName());
        }

        List<FianceTotal> fianceTotals = iFianceTotalService.selectList(entityWrapper);

        return new ResponseResult<>(fianceTotals);
    }

    @GetMapping("/detail")
    public ResponseResult detail(FianceTotal fianceTotal){

        FianceTotal fianceTotal1 = fianceTotal.selectById();

        return new ResponseResult<>(fianceTotal1);
    }

    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody FianceTotal fianceTotal){

        if(StringUtils.isEmpty(fianceTotal.getType())){
            FianceTotal fian = iFianceTotalService.selectById(fianceTotal.getId());
            fianceTotal.setType(fian.getId());
        }

        boolean insert = fianceTotal.insert();

        if (insert) {

            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody FianceTotal fianceTotal){

        boolean updateById = fianceTotal.updateById();

        if (updateById) {
            return new ResponseResult();
        } else {
            return new ResponseResult(1, "失败");
        }
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody FianceTotal fianceTotal){

        boolean deleteById = fianceTotal.deleteById();

        if (deleteById) {
            return new ResponseResult();
        } else {
        }
        return new ResponseResult(1, "失败");
    }
}
